package com.interceptor;

import com.util.ConstantUtil;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class UserSecurityInterceptor implements HandlerInterceptor {

    private List<String> fiterlist = Arrays.asList("500","404","403","ip","levelDiscount","returnRules","agentJoin","elecGame","mobileClient","zryl","lottery","promotion","helpCenter","loginSubmit", "findPassword", "register", "kaptcha", "forgotSendPhoneCode", "forgotSendEmailCode", "forgotFristStepByPhone", "forgotFristStepByEmail", "reSetPassword", "forgotpasswordone", "forgotpasswordtwo", "forgotpasswordthree");

    private List<String> usercenterPagefiter = Arrays.asList("questionDialog", "questionWrite", "systemMessageReceive", "bankList");

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestMethod = request.getRequestURI();
        for (String item : fiterlist) {
            if ((requestMethod.indexOf(item) != -1) || ("/".equalsIgnoreCase(requestMethod))) {
                return true;
            }
        }
        Object obj = request.getSession().getAttribute(ConstantUtil.loginSession);
        if (obj == null) {
            response.sendRedirect("/");
            return false;
        }

        request.setAttribute("showGames", false);
        for (String item : usercenterPagefiter) {
            if (requestMethod.indexOf(item) != -1) {
                request.setAttribute("showGames", true);
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
    }
}